package io.renren.modules.udp.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import io.renren.modules.udp.entity.R5rmObjectsEntity;
import io.renren.modules.udp.service.R5rmObjectsService;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 设备基础信息
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2020-01-13 11:58:57
 */
@RestController
@RequestMapping("udp/r5rmobjects")
public class R5rmObjectsController {
    @Autowired
    private R5rmObjectsService r5rmObjectsService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("udp:r5rmobjects:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = r5rmObjectsService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 获取项目的设备信息
     * @param objProject
     * @return
     */
    @RequestMapping("/objList/{objProject}")
    @RequiresPermissions("udp:r5rmobjects:objList")
    public R objList(@PathVariable ("objProject") String objProject) {
        Map<String,Object> params = new HashMap<>();
        params.put("obj_project",objProject);
        int zongshu = r5rmObjectsService.selectCount(new EntityWrapper<R5rmObjectsEntity>().eq("obj_project",objProject));
        int yunxing = r5rmObjectsService.selectCount(new EntityWrapper<R5rmObjectsEntity>().eq("obj_project",objProject).eq("obj_udfchar04","1"));
        int daiji = r5rmObjectsService.selectCount(new EntityWrapper<R5rmObjectsEntity>().eq("obj_project",objProject).eq("obj_udfchar04","0"));
        int guzhang = r5rmObjectsService.selectCount(new EntityWrapper<R5rmObjectsEntity>().eq("obj_project",objProject).eq("obj_udfchar04","2"));
        List<R5rmObjectsEntity> objectsEntity = r5rmObjectsService.selectByMap(params);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("r5rmObjects",objectsEntity);
        jsonObject.put("zongshu",zongshu);
        jsonObject.put("yunxing",yunxing);
        jsonObject.put("daiji",daiji);
        jsonObject.put("guzhang",guzhang);
        return R.ok().put("jsonObject",jsonObject);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{objCode}")
    @RequiresPermissions("udp:r5rmobjects:info")
    public R info(@PathVariable("objCode") Long objCode){
		R5rmObjectsEntity r5rmObjects = r5rmObjectsService.selectById(objCode);

        return R.ok().put("r5rmObjects", r5rmObjects);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("udp:r5rmobjects:save")
    public R save(@RequestBody R5rmObjectsEntity r5rmObjects){
		r5rmObjectsService.insert(r5rmObjects);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("udp:r5rmobjects:update")
    public R update(@RequestBody R5rmObjectsEntity r5rmObjects){
		r5rmObjectsService.updateById(r5rmObjects);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("udp:r5rmobjects:delete")
    public R delete(@RequestBody Long[] objCodes){
		r5rmObjectsService.deleteBatchIds(Arrays.asList(objCodes));

        return R.ok();
    }

}
